
require 'rubygems'
require 'twitter'

require 'raklur/account'
require 'raklur/stream'

module Raklur
	module Plugin
		module Twitter

			#
			# Account management for Twitter accounts
			#
			class Account < Raklur::Account
				API_SERVER = 'api.twitter.com'
				SEARCH_SERVER = 'search.twitter.com'

				OAUTH_CONSUMER_TOKEN = 'JCtKwThjDbI3cdjTWE4Jw'
				OAUTH_CONSUMER_SECRET = 'ZC5ZMm8NTXrxVdO9DWoEnvxxBvX1sKNxlFMGEMKB2fc'

				def initialize title, data
					@streams = []
					@public_timeline_enable = true
					@home_timeline_enable = true
					@friends_timeline_enable = true
					@user_timeline_enable = true
					@mentions_enable = true
					@search_enable = true
					@oauth = nil

					super title, data

					#	data['server'] = API_SERVER
					#	data['port'] = 80
					#	data['update_delay'] = 60 * 10 #seconds

					yield self if block_given?
				end


				#
				# Export current config as a hash
				#
				def export_config 
				end


				#
				#
				#
				def validate_config_arg key, value
					case key
					when 'user_timeline_enable' then
						@user_timeline_enable = value
					when 'home_timeline_enable' then
						@home_timeline_enable = value
					when 'public_timeline_enable' then
						@public_timeline_enable = value
					when 'friends_timeline_enable' then
						@friends_timeline_enable = value
					when 'mentions_enable' then
						@mentions_enable = value
					when 'search_enable' then
						@search_enable = value
					when 'search' then
					else
						super key, value
					end
				end

				def verify
					super
				end

				def enable
					if @public_timeline_enable then
						@streams << Stream.new( self, "public",
											   "http://api.twitter.com/1/statuses/public_timeline"  )
					end
					if @home_timeline_enable then
						@streams << Stream.new( self, "home",
											   "http://api.twitter.com/1/statuses/home_timeline"  )
					end
					if @friends_timeline_enable then
						@streams << Stream.new( self, "friends",
											   "http://api.twitter.com/1/statuses/friends_timeline" )
					end
					if @user_timeline_enable then
						@streams << Stream.new( self, "user",
											   "http://api.twitter.com/1/statuses/user_timeline" )
					end
					if @mentions_enable then
						@streams << Stream.new( self, "mentions",
											   "http://api.twitter.com/1/statuses/mentions" )
					end
				end

				def to_s
					str = super
					str << "  public_timeline_enable = %s\n" % @public_timeline_enable
					str << "  home_timeline_enable = %s\n" % @home_timeline_enable
					str << "  friends_timeline_enable = %s\n" % @friends_timeline_enable
					str << "  user_timeline_enable = %s\n" % @user_timeline_enable
					str << "  mentions_enable = %s\n" % @mentions_enable
					return str
				end

				def login
					config = ConfigStore.new "#{ENV['HOME']}/.raklur/netspider.config"
					config.load
					@oauth = Twitter::OAuth.new 'JCtKwThjDbI3cdjTWE4Jw', 'ZC5ZMm8NTXrxVdO9DWoEnvxxBvX1sKNxlFMGEMKB2fc'
					pp config

					if config['atoken'] && config['asecret'] then
						@oauth.authorize_from_access config['atoken'], config['asecret']
						@client = Twitter::Base.new @oauth

					elsif config['rtoken'] && config['rsecret'] && config['pin'] then
						@oauth.authorize_from_request config['rtoken'], config['rsecret'], config['pin']
						@client = Twitter::Base.new @oauth
						pp @client.friends_timeline

						config.update( {
							'atoken' => @oauth.access_token.token,
							'asecret' => @oauth.access_token.secret,
						}).delete('rtoken','rsecret')

					else
						# authorize in browser
						%x(xdg-open #{@oauth.request_token.authorize_url})
						print "PIN ? "
						pin = STDIN.gets.strip

						config.update( {
							'rtoken' => @oauth.request_token.token,
							'rsecret' => @oauth.request_token.secret,
							'pin' => pin
						})
					end
				end

				def streams
					@streams
				end

			end	
		end
	end
end
